# coding=utf-8
"""
主入口
使用click分配命令
"""
import click
from project.models import User
from project.tools import Session
from project import main
@click.group()
#@click.option('--debug/--no-debug', default=False)
def cli():
    """
    主服务
    """
    pass
    #click.echo('Debug mode is %s' % ('on' if debug else 'off'))

@cli.command()
def init():
    """
    初始化数据库
    """
    # 数据库初始化
    session = Session()
    user = session.query(User).filter_by(user_id='admin', is_deleted=False).first()
    # 初始化用户表
    if not user:
        user = User()
        user.user_id = 'admin'
        user.name = u'管理员'
        user.set_encrypt_pwd('admin')
        session.add(user)
    # 提交
    session.commit()

@cli.command()
def start():
    """
    启动服务器
    """
    click.echo('Start server')
    main()

if __name__ == '__main__':
    # pylint: disable=E1120
    cli()
